package com.waterworld.watch.chat.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.waterworld.watch.R;
import com.waterworld.watch.chat.adapter.ChatMsgAdapter;
import com.waterworld.watch.chat.adapter.CreateGroupAdapter;
import com.waterworld.watch.chat.bean.MemberSelectBean;
import com.waterworld.watch.chat.interfaces.IChatManager;
import com.waterworld.watch.chat.manager.ChatManager;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.ChatGroupBean;
import com.waterworld.watch.common.bean.GroupAllowUserBean;
import com.waterworld.watch.common.bean.GroupMemberBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditMemberActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_confirm)
    TextView header_confirm;

    @BindView(R.id.rv_member)
    RecyclerView rv_member;

    private IChatManager iChatManager = ChatManager.getInstance();
    private ChatGroupBean bean;
    private List<MemberSelectBean> mList = new ArrayList<>();
    private CreateGroupAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_member);
        ButterKnife.bind(this);
        bean = (ChatGroupBean) getIntent().getSerializableExtra("ChatGroupBean");
        initView();
    }

    public void initView() {
        setViewSize(header_parent, LinearLayout.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);
        header_confirm.setVisibility(View.VISIBLE);
        if (getIntent().getStringExtra("title").equals("add")) {
            header_title.setText(getString(R.string.add_member));
            if (iChatManager != null) {
                iChatManager.listGroupMember(bean.getGroupId(), new BaseObserverListener<BaseResultBean<GroupMemberBean[]>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResultBean<GroupMemberBean[]> bean1) {
                        List<GroupMemberBean> memberList = new ArrayList<>();
                        memberList.addAll(Arrays.asList(bean1.getData()));
                        iChatManager.listGroupAllowUser(bean.getWatchId(), new BaseObserverListener<BaseResultBean<GroupAllowUserBean[]>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BaseResultBean<GroupAllowUserBean[]> bean2) {
                                List<GroupAllowUserBean> allowList = new ArrayList<>();
                                allowList.addAll(Arrays.asList(bean2.getData()));

                                for (GroupAllowUserBean groupAllowUserBean : allowList) {
                                    boolean flag = false;
                                    for (GroupMemberBean groupMemberBean : memberList) {
                                        if (groupAllowUserBean.getUserId() == groupMemberBean.getMemberId()) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (!flag) {
                                        mList.add(new MemberSelectBean(groupAllowUserBean,false));
                                    }
                                }

                                mAdapter = new CreateGroupAdapter(EditMemberActivity.this,mList);
                                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(EditMemberActivity.this);
                                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                                rv_member.setLayoutManager(linearLayoutManager);
                                rv_member.setAdapter(mAdapter);
                            }
                        });
                    }
                });
            }
            header_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<MemberSelectBean> list = new ArrayList<>();
                    for (MemberSelectBean bean : mAdapter.getData()) {
                        if (bean.getSelect()) {
                            list.add(bean);
                        }
                    }
                    if (list.size() == 0) {
                        ToastUtils.showShort(EditMemberActivity.this, "必须要选择一个用户！");
                    } else {
                        if (iChatManager != null) {
                            Map<String,Object> map = new HashMap<>();
                            map.put("groupId",bean.getGroupId());
                            List<Map<String,Object>> member = new ArrayList<>();
                            for (MemberSelectBean bean : list) {
                                Map<String,Object> map1 = new HashMap<>();
                                map1.put("memberType",bean.getGroupAllowUserBean().getUserType());
                                map1.put("memberId",bean.getGroupAllowUserBean().getUserId());
                                map1.put("memberName",bean.getGroupAllowUserBean().getName());
                                member.add(map1);
                            }
                            map.put("groupMember",member);
                            String json = JSONObject.toJSONString(map);
                            iChatManager.addGroupMember(json, new BaseObserverListener<BaseResultBean>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(BaseResultBean baseResultBean) {
                                    if (baseResultBean.getCode() == 0) {
                                        finish();
                                    }
                                }
                            });
                        }
                    }
                }
            });
        } else if (getIntent().getStringExtra("title").equals("delete")) {
            header_title.setText(getString(R.string.delete_member));
            if (iChatManager != null) {
                iChatManager.listGroupMember(bean.getGroupId(), new BaseObserverListener<BaseResultBean<GroupMemberBean[]>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResultBean<GroupMemberBean[]> bean1) {
                        List<GroupMemberBean> memberList = new ArrayList<>();
                        memberList.addAll(Arrays.asList(bean1.getData()));
                        iChatManager.listGroupAllowUser(bean.getWatchId(), new BaseObserverListener<BaseResultBean<GroupAllowUserBean[]>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BaseResultBean<GroupAllowUserBean[]> bean2) {
                                List<GroupAllowUserBean> allowList = new ArrayList<>();
                                allowList.addAll(Arrays.asList(bean2.getData()));

                                for (GroupAllowUserBean groupAllowUserBean : allowList) {
                                    for (GroupMemberBean groupMemberBean : memberList) {
                                        if (groupAllowUserBean.getUserId() == groupMemberBean.getMemberId()) {
                                            mList.add(new MemberSelectBean(groupAllowUserBean, false));
                                        }
                                    }
                                }
                                mAdapter = new CreateGroupAdapter(EditMemberActivity.this, mList);
                                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(EditMemberActivity.this);
                                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                                rv_member.setLayoutManager(linearLayoutManager);
                                rv_member.setAdapter(mAdapter);
                            }
                        });
                    }
                });
            }
            header_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<MemberSelectBean> list = new ArrayList<>();
                    for (MemberSelectBean bean : mAdapter.getData()) {
                        if (bean.getSelect()) {
                            list.add(bean);
                        }
                    }
                    if (list.size() == 0) {
                        ToastUtils.showShort(EditMemberActivity.this, "必须要选择一个用户！");
                    } else {
                        if (iChatManager != null) {
                            Map<String,Object> map = new HashMap<>();
                            map.put("groupId",bean.getGroupId());
                            List<Map<String,Object>> member = new ArrayList<>();
                            for (MemberSelectBean bean : list) {
                                Map<String,Object> map1 = new HashMap<>();
                                map1.put("memberType",bean.getGroupAllowUserBean().getUserType());
                                map1.put("memberId",bean.getGroupAllowUserBean().getUserId());
                                map1.put("memberName",bean.getGroupAllowUserBean().getName());
                                member.add(map1);
                            }
                            map.put("groupMember",member);
                            String json = JSONObject.toJSONString(map);
                            iChatManager.deleteGroupMember(json, new BaseObserverListener<BaseResultBean>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(BaseResultBean baseResultBean) {
                                    if (baseResultBean.getCode() == 0) {
                                        finish();
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }

    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }
}
